import dayjs from 'dayjs'
import SparkMD5 from 'spark-md5'

export function formatTime(unix?: number | string | Date, template = 'YYYY-MM-DD HH:mm:ss') {
  return dayjs(unix).format(template)
}

export function md5(blob: Blob) {
  return new Promise<string>(resolve => {
    const fr = new FileReader()
    const md5 = new SparkMD5.ArrayBuffer()
    fr.onload = e => {
      md5.append(e.target.result as ArrayBuffer)
      resolve(md5.end())
    }
    fr.readAsArrayBuffer(blob)
  })
}
